#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class StaffTrainProvider : CY.GFive.Core.Providers.DALProvider.IStaffTrainProvider
	{
		#region Sql
		private static readonly string SqlSelectStaffTrain = "SELECT [Id], [Code],[TrainName],[TrainStartDate],[ConsistTime],[StopDate],[TrainSate],[TrainCate],[TrainDegree] FROM [StaffTrain] WHERE  [Id]=@Id";

		private static readonly string SqlInsertStaffTrain = "INSERT INTO [StaffTrain]([Code],[TrainName],[TrainStartDate],[ConsistTime],[StopDate],[TrainSate],[TrainCate],[TrainDegree]) values(@Code,@TrainName,@TrainStartDate,@ConsistTime,@StopDate,@TrainSate,@TrainCate,@TrainDegree);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateStaffTrain = "UPDATE [StaffTrain] SET [Code] = @Code,[TrainName] = @TrainName,[TrainStartDate] = @TrainStartDate,[ConsistTime] = @ConsistTime,[StopDate] = @StopDate,[TrainSate] = @TrainSate,[TrainCate] = @TrainCate,[TrainDegree] = @TrainDegree WHERE  [Id]=@Id";

		private static readonly string SqlDeleteStaffTrain = "DELETE [StaffTrain] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllStaffTrain = "SELECT [Id], [Code],[TrainName],[TrainStartDate],[ConsistTime],[StopDate],[TrainSate],[TrainCate],[TrainDegree] FROM [StaffTrain]";
		#endregion

		#region IStaffTrainProvider Members
		public Core.Business.StaffTrain Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectStaffTrain);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.StaffTrain staffTrain = new Core.Business.StaffTrain();

				if (!reader.IsDBNull(0)) staffTrain.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) staffTrain.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) staffTrain.TrainName = reader.GetString(2);
				if (!reader.IsDBNull(3)) staffTrain.TrainStartDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) staffTrain.ConsistTime = reader.GetDecimal(4);
				if (!reader.IsDBNull(5)) staffTrain.StopDate = reader.GetDateTime(5);
				if (!reader.IsDBNull(6)) staffTrain.TrainSate = reader.GetString(6);
				if (!reader.IsDBNull(7)) staffTrain.TrainCate = reader.GetString(7);
				if (!reader.IsDBNull(8)) staffTrain.TrainDegree = reader.GetString(8);

				return staffTrain;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.StaffTrain staffTrain)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, staffTrain.Code);
			sql.AddParameter("@TrainName", SqlDbType.NVarChar, staffTrain.TrainName);
			sql.AddParameter("@TrainStartDate", SqlDbType.DateTime, staffTrain.TrainStartDate);
			sql.AddParameter("@ConsistTime", SqlDbType.Decimal, staffTrain.ConsistTime);
			sql.AddParameter("@StopDate", SqlDbType.DateTime, staffTrain.StopDate);
			sql.AddParameter("@TrainSate", SqlDbType.NVarChar, staffTrain.TrainSate);
			sql.AddParameter("@TrainCate", SqlDbType.NVarChar, staffTrain.TrainCate);
			sql.AddParameter("@TrainDegree", SqlDbType.NVarChar, staffTrain.TrainDegree);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertStaffTrain);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) staffTrain.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.StaffTrain staffTrain)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, staffTrain.Code);
			sql.AddParameter("@TrainName", SqlDbType.NVarChar, staffTrain.TrainName);
			sql.AddParameter("@TrainStartDate", SqlDbType.DateTime, staffTrain.TrainStartDate);
			sql.AddParameter("@ConsistTime", SqlDbType.Decimal, staffTrain.ConsistTime);
			sql.AddParameter("@StopDate", SqlDbType.DateTime, staffTrain.StopDate);
			sql.AddParameter("@TrainSate", SqlDbType.NVarChar, staffTrain.TrainSate);
			sql.AddParameter("@TrainCate", SqlDbType.NVarChar, staffTrain.TrainCate);
			sql.AddParameter("@TrainDegree", SqlDbType.NVarChar, staffTrain.TrainDegree);
			sql.AddParameter("@Id", SqlDbType.Int, staffTrain.Id);
			sql.ExecuteSql(SqlUpdateStaffTrain);
		}

		public void Delete(Core.Business.StaffTrain staffTrain)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, staffTrain.Id);
			sql.ExecuteSql(SqlDeleteStaffTrain);
		}

		public IList<Core.Business.StaffTrain> GetAllStaffTrain()
		{
			IList<Core.Business.StaffTrain> staffTrainlist = new List<Core.Business.StaffTrain>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllStaffTrain);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.StaffTrain staffTrain = new Core.Business.StaffTrain();

					if (!reader.IsDBNull(0)) staffTrain.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) staffTrain.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) staffTrain.TrainName = reader.GetString(2);
					if (!reader.IsDBNull(3)) staffTrain.TrainStartDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) staffTrain.ConsistTime = reader.GetDecimal(4);
					if (!reader.IsDBNull(5)) staffTrain.StopDate = reader.GetDateTime(5);
					if (!reader.IsDBNull(6)) staffTrain.TrainSate = reader.GetString(6);
					if (!reader.IsDBNull(7)) staffTrain.TrainCate = reader.GetString(7);
					if (!reader.IsDBNull(8)) staffTrain.TrainDegree = reader.GetString(8);

					staffTrain.MarkOld();
					staffTrainlist.Add(staffTrain);
				}
				reader.Close();
			}
			return staffTrainlist;
		}
		#endregion
	}
}
